package me.aartikov.simplenavigationsample.slice;

import me.aartikov.alligator.NavigationContext;
import me.aartikov.alligator.NavigationContextBinder;
import me.aartikov.alligator.Navigator;
import me.aartikov.alligator.helpers.FractionStack;
import me.aartikov.alligator.Screen;
import me.aartikov.alligator.screenswitchers.FragmentScreenSwitcher;
import me.aartikov.simplenavigationsample.ResourceTable;
import me.aartikov.simplenavigationsample.SampleApplication;
import me.aartikov.simplenavigationsample.SampleTransitionAnimationProvider;
import me.aartikov.simplenavigationsample.screens.ScreenC;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.agp.window.service.WindowManager;

import java.util.List;

public class AbilityBSlice extends AbilitySlice {

    private Navigator mNavigator = SampleApplication.getNavigator();
    private NavigationContextBinder mNavigationContextBinder = SampleApplication.getNavigationContextBinder();
    private FractionManager fractionManager;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        super.setUIContent(ResourceTable.Layout_ability_b);
        ((Text) findComponentById(ResourceTable.Id_title)).setText(ResourceTable.String_screen_b);
        fractionManager = ((FractionAbility) getAbility()).getFractionManager();
    }

    @Override
    public void onActive() {
        super.onActive();

        NavigationContext navigationContext = new NavigationContext.Builder(getAbility(), SampleApplication.getNavigationFactory())
                .fragmentNavigation(((FractionAbility) getAbility()).getFractionManager())
                .transitionAnimationProvider(new SampleTransitionAnimationProvider())
                .screenSwitcher(new FragmentScreenSwitcher(SampleApplication.getNavigationFactory(), ((FractionAbility) getAbility()).getFractionManager()))
                .build();
        mNavigationContextBinder.bind(navigationContext);
        setInitialFragmentIfRequired();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    protected void setInitialFragmentIfRequired() {
        List<Screen> screens = FractionStack.getInstance(((FractionAbility) getAbility()).getFractionManager()).getCurrentFragmentScreen(getAbility().toString());
        if (screens.size() == 0 && mNavigator.canExecuteCommandImmediately()) {
            mNavigator.reset(new ScreenC(ResourceTable.Id_show_fragment, getAbility().toString()));
        } else {
            mNavigator.reduce(screens);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        mNavigationContextBinder.unbind(getAbility());
    }

    @Override
    public void onBackPressed() {
        if (!mNavigator.goBack()) {
            super.onBackPressed();
        }
    }
}
